package core.hxl.dao;

import core.hxl.entity.UserImei;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class UserImeiDaoImpl implements UserImeiDao{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public int addUserImei(UserImei userImei) {
        mongoTemplate.save(userImei);
        return 1;
    }

    @Override
    public int delUserImei(String _id) {
        Query query=new Query(Criteria.where("_id").is(_id));
        mongoTemplate.remove(query,UserImei.class);
        return 0;
    }

    @Override
    public int updateUserImei(UserImei userImei) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(userImei.get_id()));
        Update update = new Update();
        update.set("imei",userImei.getImei());
        update.set("user_id",userImei.getUserid());
        mongoTemplate.updateFirst(query, update, UserImei.class);
        return 1;
    }

    @Override
    public List<UserImei> getUserImeiList(Map<String, String> queryMap, Integer offset, Integer limit) {
        return mongoTemplate.findAll(UserImei.class);
    }
}
